package org.vacoor;

import org.vacoor.ui.qq.resources.UIResourceManager;

import javax.swing.*;
import java.awt.*;

/**
 * Created with IntelliJ IDEA.
 * User: vacoor
 * Date: 10/31/13
 * Time: 11:18 AM
 * To change this template use File | Settings | File Templates.
 */
public class HiButton extends JButton {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

//        if (getModel().isArmed()) {
        // 你可以选一个高亮的颜色作为圆形按钮类的属性
        //g.setColor(Color.lightGray);
            /*
            if(image!=null){
                g.drawImage(image, xs, ys, widths, heights, null);
            }
            if (!getText().equals("")) {
                g2d.drawString(getText(), xs+widths+4, ys+heights-heights/4);
            }
            */
//        } else {
            /*Paint f=new GradientPaint(0.0f, 0.0f, new Color(255, 255, 255, 100), 0,getSize().height,new Color(255, 255, 255, 0),false);*/
//            if (isheight) {
        if (true) {
            //画出多点线性渐变
            Paint oldPaint = g2d.getPaint();

            LinearGradientPaint p;

            p = new LinearGradientPaint(0.0f, 0.0f, 0.0f, 20.0f,
                    new float[]{0.0f, 0.5f, 0.501f, 1.0f},
                    new Color[]{
                            new Color(255, 255, 255, 200),
                            new Color(255, 255, 255, 0),
                            new Color(255, 255, 255, 0),
                            new Color(255, 255, 255, 200)
                    }
            );
            g2d.setPaint(p);
            //g2d.fillRect(0, 0, getWidth(), 21);
//                g2d.fillRoundRect(1, 1, getWidth()-4, getHeight()-4, 1, 1);
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(new Color(255, 255, 255, 200));
                /*
                 * g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
                 */
            //画出高光部分
            g.drawRoundRect(1, 1, getSize().width - 4, getSize().height - 4, 1,
                    1);
            g2d.setPaint(oldPaint);
        }

            /*
            if(image!=null){
                if (flag) {
                    AlphaComposite ac2 = AlphaComposite.getInstance(
                            AlphaComposite.SRC_OVER, 0.5f);
                    g2d.setComposite(ac2);
                }
                g2d.drawImage(image, xs, ys, widths, heights, null);
                if (!getText().equals("")) {
                    g2d.drawString(getText(), xs+widths+4, ys+heights-heights/4);
                }
            }
            */
        g.setColor(getBackground());
//        }


        //这个调用会画一个标签和焦点矩形。
//        super.paintComponent(g);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        HiButton btn = new HiButton();
//        btn.setOpaque(false);
        btn.setContentAreaFilled(false);
        btn.setBorder(null);
        btn.setSize(50, 50);
        panel.setBackground(Color.BLUE);
        ImageIcon icon = UIResourceManager.getMPassKeyboardImg();
        btn.setIcon(icon);
        panel.add(btn);

        frame.add(panel);
        frame.setSize(100, 300);
        frame.getContentPane().setBackground(Color.BLUE);
        frame.setDefaultCloseOperation(3);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
